﻿#include "QUnits.h"
#include "CAppUiConfig.h"
#include "CAppComm.h"
#include "QWidgetBase.h"

int  			QUnits::m_Net4GErrcount = 0;

QUnits::QUnits()
{
	m_Net4GErrcount = 0;
}

QUnits::~QUnits()
{
	
}


//获取当前时间
QString QUnits::GetCurrentTimer(QString format)
{
	if(format.isEmpty())
		return "";
   QDateTime current_date_time = QDateTime::currentDateTime();
   return current_date_time.toString(format);
}

//获取当前日期
QString QUnits::GetCurrentDate(QString spacer, T_DateMode dateMode)
{
   QDateTime current_date_time = QDateTime::currentDateTime();
   QString y_spacer = "";
   QString m_spacer = "";
   QString d_spacer = "";
   if(spacer.isEmpty())
   {
	   y_spacer = QString("年");
	   m_spacer = QString("月");
	   d_spacer = QString("日");
   }
   else
   {
	   y_spacer = spacer;
	   m_spacer = spacer;
	   d_spacer = spacer;
   }

   switch(dateMode){
   case dateMode_YMD:{
	   QString stu = QString("%1").arg(current_date_time.toString("yyyy") + y_spacer);
	   stu +=  		 QString("%1").arg(current_date_time.toString("MM") + m_spacer);
	   stu +=  		 QString("%1").arg(current_date_time.toString("dd") + (spacer.isEmpty()?d_spacer:""));
	   return stu;
   }
   case dateMode_DMY:{
	   QString stu = QString("%1").arg(current_date_time.toString("dd") + d_spacer);
	   stu +=  		 QString("%1").arg(current_date_time.toString("MM") + m_spacer);
	   stu += 		 QString("%1").arg(current_date_time.toString("yyyy") + (spacer.isEmpty()?y_spacer:""));
	   return stu;
   }
   case dateMode_YM:{
	   QString stu = QString("%1").arg(current_date_time.toString("yyyy") + y_spacer);
	   stu +=  		 QString("%1").arg(current_date_time.toString("MM") + (spacer.isEmpty()?m_spacer:""));
	   return stu;
   }
   case dateMode_MD:{
	   QString stu = QString("%1").arg(current_date_time.toString("MM") + m_spacer);
	   stu +=  		 QString("%1").arg(current_date_time.toString("dd") + (spacer.isEmpty()?d_spacer:""));
	   return stu;
   }
   default:
	   return "";

   }

}

//获取当前周几
QString QUnits::GetCurrentWeek()
{
	QDateTime current_date_time = QDateTime::currentDateTime();
	QString week = current_date_time.toString("ddd");

	if(week == QString("Mon"))
	{
//	   return TIMER_MONDAY;
	}
	else if(week == QString("Tue"))
	{
//	   return TIMER_TUESDAY;
	}
	else if(week == QString("Wed"))
	{
//	   return TIMER_WEDNESDAY;
	}
	else if(week == QString("Thu"))
	{
//	   return TIMER_THURSDAY;
	}
	else if(week == QString("Fri"))
	{
//	   return TIMER_FRIDAY;
	}
	else if(week == QString("Sat"))
	{
//	   return TIMER_SATURDAY;
	}
	else if(week == QString("Sun"))
	{
//	   return TIMER_SUNDAY;
	}
	return "";
}

void QUnits::drawectanglRe(QPainter *painter, int x, int y, int w, int h,QColor colour)
{
    painter->save();

    QPen pen;
    pen.setColor(colour);
    pen.setCapStyle(Qt::FlatCap);
    pen.setCosmetic(true);
    pen.setWidthF(4);
    painter->setPen(pen);
    QPainterPath p;
    p.moveTo(x,y);
    p.lineTo(x+w,y);//->
    p.lineTo(x+w,y+h);//|
    p.lineTo(x,y+h);//<-
    p.lineTo(x,y);//|
    painter->drawPath(p);
    painter->setRenderHints(QPainter::SmoothPixmapTransform);//消锯齿
//    painter->fillPath(p,colour);
    painter->restore();
}


/**
 * 获取指定目录下指定文件类型的所有文件
 *
 * @param - in dir 目录路径
 * @param - in filter 指定文件类型（可以多选）
 * @note -
 * @return - 文件列表
 */
QList<QFileInfo> QUnits::getDirSpecifyTypeAllFile(QString dirPath, QStringList filter)
{

	QDir dir(dirPath);
    dir.setNameFilters(filter);
    QList<QFileInfo> fileInfo(dir.entryInfoList(filter));
    return fileInfo;
}

/**
 * 文字转图片
 *
 * @param - in text 文字
 * @param - in imgW 图片宽度
 * @param - in imgH 图片高度
 * @param - in wrap 换行符
 * @note -
 * @return - jpg文件路径
 */
string QUnits::textToImg(string text, int imgW, int imgH, const string wrap)
{
	QString savePath = "/tmp/1.jpg";
    QString strYear = QString::fromLocal8Bit(text.data());
    QImage image = QImage(imgW,imgH,QImage::Format_ARGB32);
    image.fill(QColor(19,90,161));

    QPainter p;
    p.setRenderHint(QPainter::Antialiasing);
    p.begin(&image);
    p.setPen(Qt::white);

    QFont font;
    font.setPixelSize(50);
    font.setBold(true);
    p.setFont(font);

	QFontMetrics fm(font);
	QRect rec = fm.boundingRect("获取文字最大宽度");// 获取文字最大宽度
	int tipsH = rec.height();


	int tipsW = rec.width() / QString("获取文字最大宽度").size();

	int showNum = (imgW - 80) / tipsW;

	int drawCount = 1;
	QStringList textList = strYear.split(QString::fromStdString(wrap));

	qDebug() << textList;

	for(auto text_i : textList){
		qDebug() << text_i;
		while(text_i.size() * tipsW > imgW){
			// 换行处理
			int tmpShowNum = showNum;
			QString tmpText = text_i.mid(0, tmpShowNum);

			// 如果不止包含了中文，英文或数字要小一些,可以多显示
			while(fm.boundingRect(tmpText + "测测").width() < imgW){
				tmpShowNum++;
				tmpText = text_i.mid(0, tmpShowNum);
				if(tmpShowNum > text_i.size()){
					break;
				}
			}


			text_i.remove(tipsW, tmpText.size());
			qDebug() << tmpText;
			qDebug() << text_i;
			p.drawText(tipsW + 50,drawCount * tipsH + 50,tmpText);
			drawCount++;
		}
		if(!text_i.isEmpty()){
			p.drawText(tipsW + 50,drawCount * tipsH + 50,text_i);
			drawCount++;
		}
	}

    p.end();
    image.save(savePath);
	return savePath.toStdString();
}

// 判断OK键是否正确
bool QUnits::isOk(int key)
{
	return (key == QT_OK) || (key == Qt::Key_Enter);
}

// 判断Clean键是否正确
bool QUnits::isClean(int key)
{
	return (key == QT_CLEAR) || (key == Qt::Key_Minus)  || (key == Qt::Key_Asterisk);
}


// 年月日转时间戳
bool QUnits::ymdToTimestamp(int y, int m, int d, uint16_t &outData)
{
	bool ret = false;

	QString str = QString("%1-%2-%3 23:59:59").arg(y, 4, 10 ,QLatin1Char('0')).arg(m, 2, 10 ,QLatin1Char('0')).arg(d, 2, 10 ,QLatin1Char('0'));
	QDateTime dataTime = QDateTime::fromString(str, "yyyy-MM-dd hh:mm:ss");
	qint64 timestatmp = dataTime.toSecsSinceEpoch();
	qDebug() << timestatmp;
	if(!dataTime.isValid()){
		return false;
	}
	if(timestatmp > 0){
		ret = true;
	}
	outData = timestatmp / 86400;
	return ret;
}


// 年月日转时间戳
bool QUnits::ymdToTimestamp(int y, int m, int d,int h, int min, int s, uint64_t &outData)
{
	bool ret = false;

	QString str = QString("%1-%2-%3 %4:%5:%6")
			.arg(y, 4, 10 ,QLatin1Char('0'))
			.arg(m, 2, 10 ,QLatin1Char('0'))
			.arg(d, 2, 10 ,QLatin1Char('0'))
			.arg(h, 2, 10 ,QLatin1Char('0'))
			.arg(min, 2, 10 ,QLatin1Char('0'))
			.arg(s, 2, 10 ,QLatin1Char('0'));
	QDateTime dataTime = QDateTime::fromString(str, "yyyy-MM-dd hh:mm:ss");
	if(!dataTime.isValid()){
		return false;
	}
	qint64 timestatmp = dataTime.toSecsSinceEpoch();
	qDebug() << timestatmp;
	if(timestatmp > 0){
		ret = true;
	}
	outData = timestatmp;
	return ret;
}
